<template>
  <div class="app-container" :style="'height:' + height + 'px'">
    <div style="width: 100%; height: 100%" v-html="poHtmlCode"></div>
    <input type="hidden" name="contractId" id="contractId" />
    <input type="hidden" name="fileId" id="fileId" />
    <input type="hidden" name="fileName" id="fileName" />
  </div>
</template>

<script setup name="Index">

import {
  viewFormByTemplateId,
  viewFormByFileId
} from "@/api/contract/check";
const search = window.location.search;
const poHtmlCode = ref("");
const height = ref("");
const contractId = ref();
const fileId = ref();
const fileName = ref();

// const { proxy } = getCurrentInstance();

if (search) {
  const searchList = search.substring(1).split("&");
  if (searchList[0].indexOf("fileId") != -1) {
    fileId.value = searchList[0].split(":")[1];
    fileName.value = searchList[1].split(":")[1];
    viewFormByFileId({ fileId: fileId.value, fileName: fileName.value }).then((res) => {
      poHtmlCode.value = res;
    });
  } else {
    contractId.value = searchList[0].split(":")[1];
    const templateId = searchList[1].split(":")[1];
    viewFormByTemplateId({ contractId: contractId.value, templateId: templateId }).then((res) => {
      poHtmlCode.value = res;
    });
  }
}

function OnPageOfficeCtrlInit() {
  // PageOffice的初始化事件回调函数，您可以在这里添加自定义按钮
  pageofficectrl.AddCustomToolButton("保存", "Save(1)", 1);
}
function AfterDocumentOpened() {
  // PageOffice的文档打开后事件回调函数
  pageofficectrl.DisableSave = true;
}
function BeforeDocumentSaved() {
  // PageOffice的文档保存前事件回调函数
}
function AfterDocumentSaved() {
  // PageOffice的文档保存后事件回调函数
  if (pageofficectrl.CustomSaveResult) {
    window.external.close();
    var sResult = window.external.CallParentFunc("handleQueryContractFile('" + pageofficectrl.CustomSaveResult + "')");
    if (sResult !== "poerror:parentlost") {
      window.external.CallParentFunc("handleQueryContractFile('" + pageofficectrl.CustomSaveResult + "')");
    }
    alert("保存成功！");
  } else {
    alert("保存失败！");
  }
}

function Save() {
  if (contractId.value) {
    document.getElementById("contractId").value = contractId.value;
  } else {
    document.getElementById("fileId").value = fileId.value;
  }
  pageofficectrl.WebSave();
}


onMounted(() => {
  window.Save = Save;
  // 以下的为PageOffice事件的回调函数，名称不能改，否则PageOffice控件调用不到
  window.OnPageOfficeCtrlInit = OnPageOfficeCtrlInit;
  window.AfterDocumentOpened = AfterDocumentOpened;
  window.BeforeDocumentSaved = BeforeDocumentSaved;
  window.AfterDocumentSaved = AfterDocumentSaved;
  var box = document.querySelector(".app-container"); // 根据你的盒子ID选择器进行修改
  var boxDistanceFromTop = box.getBoundingClientRect().top; // 盒子距离顶部的距离
  height.value = window.innerHeight - boxDistanceFromTop - 21; // 视窗口高度减去盒子距离顶部的距离
});
</script>

<style scoped lang="scss"></style>
